import {CN,EN} from '../config'
const Mixin = {
	isValidFn(nv){
		try{
			//test fn valid or not
			let fn = new Function("return "+nv.fnExpr)()
			fn(100)
			return true
		}catch(e){
			return false
		}
	},
	getById(id){
		return this.cacheRs.filter(r=>r.id===id)[0]
	},
	isItemWrapFocus(t){
		let cn = this.cn
		let pnode = t.parentNode
		while(pnode!==this.$dom){
			if(pnode.classList.contains(this.cn.itemWrap))return pnode
			pnode = pnode.parentNode
		}
	},
	eyeToggle(e){
		e.target.query("input[type=checkbox]").click()
	},
	focusItems(ids){
		let fs = this.$dom.querySelectorAll("."+CN.focus)
		for(let i=0;i<fs.length;i++)
			fs[i].classList.remove(CN.focus)
		ids.forEach((id,i)=>{
			let item = this.$dom.query("#"+id)
			if(item)item.classList.add(CN.focus)
			if(i===0&&item)item.scrollIntoViewIfNeeded()
		})
	},
	removeItems(ids){
		this.cacheRs = this.cacheRs.filter(r=>!ids.includes(r.id))
		ids.forEach(id=>{
			let item = this.$dom.query("#"+id)
			if(item)item.remove()
		})
	},
	onItemClick(e){
		if(!this.active)return
		let t= e.target
		let itemWrap = this.isItemWrapFocus(t)
		if(!itemWrap)return
		if(!itemWrap.classList.contains(CN.focus))
			this.emit(EN.onLeftItemFocus,itemWrap.id)
		this.focusItems([itemWrap.id])
	}
}
export default Mixin